﻿using System;
using System.Collections.Generic;
using System.Text;
using ApiCoreProject.Model.Entities;
using ApiCoreProject.Bll.Interface;
using ApiCoreProject.Dal;
using System.Dynamic;
using System.Linq;

namespace ApiCoreProject.Bll.Implement
{
    class AuthorityServe : BaseBll<Authority>, IAuthorityServe
    {
        public AuthorityServe(IBaseDal<Authority> dal) : base(dal)
        {
        }

        /// <summary>
        /// 获取角色菜单列表
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public dynamic GetAuthorityMenusByRoleID(int roleID)
        {
            var authority = dal.GetModels(a => a.RoleId == roleID);

            var menu = (from a in authority
                        where a.Menu.ParentId == 0
                        select new
                        {
                            a.Menu.MenuId,
                            a.Menu.MenuName,
                            a.Menu.HrefUrl,
                            a.Menu.VueUrl,
                            a.Menu.ParentId,
                            a.Menu.Icon,
                            subMenu =
                            (from s in authority
                             where a.Menu.MenuId == s.Menu.ParentId
                             select new
                             {
                                 s.Menu.MenuId,
                                 s.Menu.MenuName,
                                 s.Menu.HrefUrl,
                                 s.Menu.VueUrl,
                                 s.Menu.ParentId,
                                 s.Menu.Icon
                             }).ToList()
                        }).ToList();

            dynamic d = new ExpandoObject();
            d.count = authority.Count();
            d.menus = menu;

            return d;
        }
    }
}
